package com.google.gwt.homepage.smile.client.view;


import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler; 
import com.google.gwt.event.dom.client.HasClickHandlers;



import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DateBox.Format;
import com.google.gwt.homepage.smile.client.presenter.SearchFriendPresenter;
import com.google.gwt.homepage.smile.shared.FriendTable;
import com.google.gwt.homepage.smile.shared.SajuDataTable;

import com.google.gwt.homepage.smile.shared.SajuData;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.i18n.client.LocaleInfo;

import com.google.gwt.homepage.smile.shared.EnneagramQuestions;

import java.util.ArrayList;
import java.util.List;

public class SearchFriendView extends Composite implements SearchFriendPresenter.Display {
  
	
	
	/**
	   * The constants used in this Widget.
	   */
	  
	  public static interface CwConstants extends Constants {
		  
		String searchFriendTitle();
		String searchFriendDesc();
		
		String[] addrSido();//시도
		  
		  String[] sexDatas();//남여
		  String[] jobs();//직업 
		  String[]  sajuTimeDatas();
		
		
	  }
	
	  private CwConstants constants;
	  
	  
	  
	  
	  
	  
	  
	  private FlexTable searchFriendTable;
	  
	//private final Button addButton;
  //private final Button deleteButton;
  //private FlexTable contactsTable;
	  private final FlexTable contentTable;//가장 밖 테이블
 
  //private TextBox yearTxt;
  //private TextBox monthTxt;
  //private TextBox dateTxt;
  private static String loadingImage;
  private HTML loadingImg;
  private HTML message;
  
  
  private FlexTable inputSajuTable;
  
  private Button btnConfirm;
  
  final ListBox yearBox;
  final ListBox monthBox;
  final ListBox dateBox;
  final ListBox timeBox;
  final ListBox sexBox;
  final ListBox jobBox;
  final ListBox addrBox;
  
  
  /**
   * The default style name.
   */
  private static final String DEFAULT_STYLE_NAME = "sc-ContentWidget";
	
  
  public SearchFriendView(CwConstants constants) {
	  
	  this.constants=constants;
	  
	  
 
	VerticalPanel contentTableDecorator = new VerticalPanel();
    initWidget(contentTableDecorator);
    contentTableDecorator.setWidth("100%");
    
    
    
    
    
    
    
    //타이틀, 설명
    contentTable = new FlexTable();
    contentTable.setWidth("100%");
    contentTable.getCellFormatter().addStyleName(0, 0, "contacts-ListContainer");
    contentTable.getCellFormatter().setWidth(0, 0, "100%");
    contentTable.getFlexCellFormatter().setVerticalAlignment(0, 0, DockPanel.ALIGN_TOP);
    
    
    //
    // Create submenu, description
    VerticalPanel widgetVpanel = new VerticalPanel();
    contentTable.setWidget(0, 0, widgetVpanel);

    // Add the name
    HTML nameWidget = new HTML(constants.searchFriendTitle());
    nameWidget.setStyleName(DEFAULT_STYLE_NAME + "-name");
    widgetVpanel.add(nameWidget);

    // Add the description
    //HTML descWidget = new HTML("You can test Enneagram( total 180 question.  It takes about 15minute.)");
    HTML descWidget = new HTML(constants.searchFriendDesc());
    descWidget.setStyleName(DEFAULT_STYLE_NAME + "-description");
    widgetVpanel.add(descWidget);
    
    
    
    
    
    //생년월일시 입력부분
    inputSajuTable = new FlexTable();
    
    inputSajuTable.addStyleName("inputSajuTable");
    
    inputSajuTable.setHTML(0, 0, "생일입력");
    inputSajuTable.getCellFormatter().addStyleName(0, 0, "inputSajuHeader");
    inputSajuTable.setHTML(0, 1, "시간입력");
    inputSajuTable.getCellFormatter().addStyleName(0, 1, "inputSajuHeader");
    inputSajuTable.setHTML(0, 2, "성별");
    inputSajuTable.getCellFormatter().addStyleName(0, 2, "inputSajuHeader");
    inputSajuTable.setHTML(0, 3, "직업");
    inputSajuTable.getCellFormatter().addStyleName(0, 3, "inputSajuHeader");
    inputSajuTable.setHTML(0, 4, "사는곳");
    inputSajuTable.getCellFormatter().addStyleName(0, 4, "inputSajuHeader");
    
    
     btnConfirm = new Button("저장");
    inputSajuTable.setWidget(0, 5, btnConfirm);
    inputSajuTable.getCellFormatter().addStyleName(0, 5, "inputSajuConfirm");
    HorizontalPanel birthPanel = new HorizontalPanel();
    
    birthPanel.add(new Label("양력 "));
    
    yearBox = new ListBox();
    for (int i = 1881, j=0; i < 2050; i++,j++) {
    	yearBox.addItem(Integer.toString(i));
    	yearBox.setValue(j, Integer.toString(i));
    }
    yearBox.setItemSelected(100, true);//default 1981년 선택
    
   
    monthBox = new ListBox();
    for (int i = 1, j=0; i < 13; i++,j++) {
    	monthBox.addItem(Integer.toString(i));
    	monthBox.setValue(j, Integer.toString(i));
    }
    
   
    dateBox = new ListBox();
    for (int i = 1, j=0; i < 32; i++,j++) {
    	dateBox.addItem(Integer.toString(i));
    	dateBox.setValue(j, Integer.toString(i));
    }
    
    birthPanel.add(yearBox);
    birthPanel.add(new HTML("년 "));
    birthPanel.add(monthBox);
    birthPanel.add(new HTML("월 "));
    birthPanel.add(dateBox);
    birthPanel.add(new HTML("일"));
    
    inputSajuTable.setWidget(1, 0, birthPanel);
    inputSajuTable.getCellFormatter().addStyleName(1, 0, "inputSajuBirth");
    
    timeBox = new ListBox();
    String[] listTypes = constants.sajuTimeDatas();
    for (int i = 0; i < listTypes.length; i++) {
    	timeBox.addItem(listTypes[i]);
    	timeBox.setValue(i, Integer.toString(i));//값을 0~11로 설정(23~1시(0), 1~3시(1)...)
    }
    inputSajuTable.setWidget(1, 1, timeBox);
    inputSajuTable.getCellFormatter().addStyleName(1, 1, "inputSajuBirth");
    
    
    sexBox = new ListBox();
    String[] sexTypes = constants.sexDatas();
    for (int i = 0; i < sexTypes.length; i++) {
    	sexBox.addItem(sexTypes[i]);
    	sexBox.setValue(i, Integer.toString(i+1));//남자1, 여자2
    }
    inputSajuTable.setWidget(1, 2, sexBox);
    inputSajuTable.getCellFormatter().addStyleName(1, 2, "inputSajuBirth");
    
    
    
    jobBox = new ListBox();
    String[] jobs = constants.jobs();
    for (int i = 0; i < jobs.length; i++) {
    	jobBox.addItem(jobs[i]);
    	jobBox.setValue(i, jobs[i]);//남자1, 여자2
    }
    
    inputSajuTable.setWidget(1, 3, jobBox);
    inputSajuTable.getCellFormatter().addStyleName(1, 3, "inputSajuBirth");
    
    
   addrBox = new ListBox();
    String[] addrSido = constants.addrSido();
    for (int i = 0; i < addrSido.length; i++) {
    	addrBox.addItem(addrSido[i]);
    	addrBox.setValue(i, addrSido[i]);//남자1, 여자2
    }
    inputSajuTable.setWidget(1, 4, addrBox);
    inputSajuTable.getCellFormatter().addStyleName(1, 4, "inputSajuBirth");
    
    
    FlexCellFormatter cellFormatter = inputSajuTable.getFlexCellFormatter();
    cellFormatter.setRowSpan(0, 5, 2);
    
   
    inputSajuTable.setVisible(false);
    
    
    
    
    
    
    
    
    
    
    //친구찾기
    // Create a grid
    
    searchFriendTable = new FlexTable();
    searchFriendTable.setVisible(false);
    searchFriendTable.setWidget(0, 0, new HTML("순위"));
    searchFriendTable.setWidget(0, 1, new HTML("점수"));
    searchFriendTable.setWidget(0, 2, new HTML("성별"));
    searchFriendTable.setWidget(0, 3, new HTML("채팅"));
    searchFriendTable.setWidget(0, 4, new HTML("이메일"));
    searchFriendTable.setWidget(0, 5, new HTML("사는곳"));
    searchFriendTable.setWidget(0, 6, new HTML("직업"));
    
    searchFriendTable.addStyleName("searchFriendTable");
    searchFriendTable.getRowFormatter().addStyleName(0, "title");
    
   
   
    

 
    
    
    // Show the loading image
    if (loadingImage == null) {
      loadingImage = "<img src=\"" + GWT.getModuleBaseURL()
          + "images/loading.gif\">";
    }
    
    loadingImg = new HTML(loadingImage);
    loadingImg.setVisible(false);
    
    
    message = new HTML("");
    message.setVisible(false);
    
    contentTable.setWidget(1, 0, searchFriendTable);//친구찾기
    //contentTable.setWidget(2, 0, resultSajuTable);//사주 결과부분

    contentTable.setWidget(2, 0, loadingImg);//로딩 이미지
    
    contentTable.setWidget(3, 0, message);//메시지
    
    contentTable.setWidget(4, 0, inputSajuTable);//생년월일 입력
    
    
    
    contentTableDecorator.add(contentTable);
    
    
    
    
 
/*
    contentTable = new FlexTable();
    contentTable.setWidth("100%");
    contentTable.getCellFormatter().addStyleName(0, 0, "contacts-ListContainer");
    contentTable.getCellFormatter().setWidth(0, 0, "100%");
    contentTable.getFlexCellFormatter().setVerticalAlignment(0, 0, DockPanel.ALIGN_TOP);

    VerticalPanel widgetVpanel = new VerticalPanel();
    contentTable.setWidget(0, 0, widgetVpanel);

 
    HTML nameWidget = new HTML(constants.enneagramTestTitle());
    nameWidget.setStyleName(DEFAULT_STYLE_NAME + "-name");
    widgetVpanel.add(nameWidget);

 
    HTML descWidget = new HTML(constants.enneagramTestDesc());
    descWidget.setStyleName(DEFAULT_STYLE_NAME + "-description");
    widgetVpanel.add(descWidget);
    

    contentArea= new SimplePanel();
    contentTable.setWidget(1, 0, contentArea);
    contentTableDecorator.add(contentTable);
*/   
  }
  
  
  
  
  
  /*
  public HasClickHandlers getAddButton() {
    return addButton;
  }
  
  public HasClickHandlers getDeleteButton() {
    return deleteButton;
  }
  
  public HasClickHandlers getList() {
    return contactsTable;
  }

  public void setData(List<String> data) {
    contactsTable.removeAllRows();
    
    for (int i = 0; i < data.size(); ++i) {
      contactsTable.setWidget(i, 0, new CheckBox());
      contactsTable.setText(i, 1, data.get(i));
    }
  }
  
  public int getClickedRow(ClickEvent event) {
    int selectedRow = -1;
    HTMLTable.Cell cell = contactsTable.getCellForEvent(event);
    
    if (cell != null) {
      // Suppress clicks if the user is actually selecting the 
      //  check box
      //
      if (cell.getCellIndex() > 0) {
        selectedRow = cell.getRowIndex();
      }
    }
    
    return selectedRow;
  }
  
  public List<Integer> getSelectedRows() {
    List<Integer> selectedRows = new ArrayList<Integer>();
    
    for (int i = 0; i < contactsTable.getRowCount(); ++i) {
      CheckBox checkBox = (CheckBox)contactsTable.getWidget(i, 0);
      if (checkBox.getValue()) {
        selectedRows.add(i);
      }
    }
    
    return selectedRows;
  }
  */
  
 
  


  public void invisibleSearchFriendResult(){
	  searchFriendTable.setVisible(false);
  }
  
  
  public void setLoadImg(boolean result){//로드 이미지 설정
	  loadingImg.setVisible(result);
  }
  
  public void setMessage(String str){//메시지 설정
	  message.setHTML(str);
	  }
  
  public void setVisibleMessage(boolean result){//메시지 보임 설정
	  message.setVisible(result);
  }
  
  public void setVisibleInputSaju(boolean result){//생일입력 보임 설정
	  inputSajuTable.setVisible(result);
  }
  
  
  

  public HasClickHandlers getSaveBirthButton(){
	  return btnConfirm;
  }

  
  
//결과창 보여줌
  public void showSearchFriendResult(List<FriendTable> result){
	  String sex="";
	    if(result.size()>0){
		    for (int i=0; i < result.size() && i<10 ;i++) {
		    	FriendTable friend=result.get(i);
		    	searchFriendTable.setWidget(i+1, 0, new HTML(Integer.toString(i+1)));
		    	searchFriendTable.setWidget(i+1, 1, new HTML(Integer.toString(friend.getScore())));
		    	if(friend.getSex().equals("1")){
		    		sex="남";
		    	}else if(friend.getSex().equals("2")){
		    		sex="여";
		    	}
		    	
		    	searchFriendTable.setWidget(i+1, 2, new HTML(sex));
		    	searchFriendTable.setWidget(i+1, 3, new Button("채팅하기"));
		    	searchFriendTable.setWidget(i+1, 4, new Button("이메일 보내기"));
		    	searchFriendTable.setWidget(i+1, 5, new HTML(friend.getAddr1()));
		    	searchFriendTable.setWidget(i+1, 6, new HTML(friend.getJob()));
		    }
		    
		   
	    }else{
	    	FlexCellFormatter cellFormatter = searchFriendTable.getFlexCellFormatter();
	    	cellFormatter.setColSpan(1, 0, 7);
	    	searchFriendTable.setWidget(1, 0, new HTML("친구를 찾지 못했습니다."));
	    }
	   
	    //로딩 이미지 보이지 않게
		  loadingImg.setVisible(false);
		  
		  //결과가 보이게 설정
		  searchFriendTable.setVisible(true);
	    
  }  
  
  
  
  public String getInputedYear(){
	  return yearBox.getValue(yearBox.getSelectedIndex());
  }
  public String getInputedMonth(){
	  return monthBox.getValue(monthBox.getSelectedIndex());
  }
  public String getInputedDate(){
	  return dateBox.getValue(dateBox.getSelectedIndex());
  }
  public String getInputedTime(){
	  return timeBox.getValue(timeBox.getSelectedIndex());  
  }
  public String getInputedSex(){
	  return sexBox.getValue(sexBox.getSelectedIndex());  
  }
  public String getInputedJob(){
	  return jobBox.getValue(jobBox.getSelectedIndex());  
  }
  public String getInputedAddr(){
	  return addrBox.getValue(addrBox.getSelectedIndex());  
  }
  
  
  public Widget asWidget() {
    return this;
  }
}
